/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package da;

import da.exceptions.NonexistentEntityException;
import da.exceptions.PreexistingEntityException;
import entities.Transfer;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entities.Account;

/**
 *
 * @author QUY_THI
 */
public class TransferJpaController {

    public TransferJpaController() {
        emf = Persistence.createEntityManagerFactory("eBanking-ejbPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Transfer transfer) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Account account = transfer.getAccount();
            if (account != null) {
                account = em.getReference(account.getClass(), account.getAcountCode());
                transfer.setAccount(account);
            }
            Account account1 = transfer.getAccount1();
            if (account1 != null) {
                account1 = em.getReference(account1.getClass(), account1.getAcountCode());
                transfer.setAccount1(account1);
            }
            em.persist(transfer);
            if (account != null) {
                account.getTransferList().add(transfer);
                account = em.merge(account);
            }
            if (account1 != null) {
                account1.getTransferList().add(transfer);
                account1 = em.merge(account1);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTransfer(transfer.getTransferID()) != null) {
                throw new PreexistingEntityException("Transfer " + transfer + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Transfer transfer) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transfer persistentTransfer = em.find(Transfer.class, transfer.getTransferID());
            Account accountOld = persistentTransfer.getAccount();
            Account accountNew = transfer.getAccount();
            Account account1Old = persistentTransfer.getAccount1();
            Account account1New = transfer.getAccount1();
            if (accountNew != null) {
                accountNew = em.getReference(accountNew.getClass(), accountNew.getAcountCode());
                transfer.setAccount(accountNew);
            }
            if (account1New != null) {
                account1New = em.getReference(account1New.getClass(), account1New.getAcountCode());
                transfer.setAccount1(account1New);
            }
            transfer = em.merge(transfer);
            if (accountOld != null && !accountOld.equals(accountNew)) {
                accountOld.getTransferList().remove(transfer);
                accountOld = em.merge(accountOld);
            }
            if (accountNew != null && !accountNew.equals(accountOld)) {
                accountNew.getTransferList().add(transfer);
                accountNew = em.merge(accountNew);
            }
            if (account1Old != null && !account1Old.equals(account1New)) {
                account1Old.getTransferList().remove(transfer);
                account1Old = em.merge(account1Old);
            }
            if (account1New != null && !account1New.equals(account1Old)) {
                account1New.getTransferList().add(transfer);
                account1New = em.merge(account1New);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = transfer.getTransferID();
                if (findTransfer(id) == null) {
                    throw new NonexistentEntityException("The transfer with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transfer transfer;
            try {
                transfer = em.getReference(Transfer.class, id);
                transfer.getTransferID();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The transfer with id " + id + " no longer exists.", enfe);
            }
            Account account = transfer.getAccount();
            if (account != null) {
                account.getTransferList().remove(transfer);
                account = em.merge(account);
            }
            Account account1 = transfer.getAccount1();
            if (account1 != null) {
                account1.getTransferList().remove(transfer);
                account1 = em.merge(account1);
            }
            em.remove(transfer);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Transfer> findTransferEntities() {
        return findTransferEntities(true, -1, -1);
    }

    public List<Transfer> findTransferEntities(int maxResults, int firstResult) {
        return findTransferEntities(false, maxResults, firstResult);
    }

    private List<Transfer> findTransferEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Transfer.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Transfer findTransfer(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Transfer.class, id);
        } finally {
            em.close();
        }
    }

    public int getTransferCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Transfer> rt = cq.from(Transfer.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
